package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.model.dto.SysBusinessOrganizationDto;
import com.yungu.swift.system.sys.model.dto.SysPlatformDto;
import com.yungu.swift.system.sys.model.param.AdminCommissionParam;
import com.yungu.swift.system.sys.model.param.AdminPlatformParam;
import com.yungu.swift.system.sys.service.SysBusinessOrganizationService;
import com.yungu.swift.system.sys.service.SysPlatformService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/20
 */
@RestController
@RequestMapping("/admin/v1/operation/platform")
@Api(value = "【运营管理】", tags = "平台基本信息")
public class PlatformAdmin {

    /**
     * 平台级
     */
    private final static Integer ORGANIZATION_TYPE_APP = 1;

    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;

    @Reference
    private SysPlatformService sysPlatformService;

    @ResponseImgOverride
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value = "获取平台信息", httpMethod = "GET")
    public ResponseData<SysPlatformDto> get(@RequestBody AdminParam adminParam) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("appid", adminParam.getAppid());
        map.put("agentUuid", adminParam.getUserAgentUuid());
        map.put("companyUuid", adminParam.getUserCompanyUuid());
        return sysPlatformService.get(map);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "平台信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminPlatformParam platformParam) {
        SysPlatformDto sysPlatformDto = new SysPlatformDto();
        BeanUtils.copyProperties(platformParam, sysPlatformDto);
        if (StringUtils.isEmpty(sysPlatformDto.getUuid())) {
            sysPlatformDto.setUuid(StringUtils.buildUUID());
            sysPlatformDto.setCreateBy(platformParam.getUserUuid());
            sysPlatformDto.setAppid(platformParam.getAppid());
            return sysPlatformService.add(sysPlatformDto);
        }
        sysPlatformDto.setUpdateBy(platformParam.getUserUuid());
        return sysPlatformService.edit(sysPlatformDto);
    }

    @RequestMapping(value = "/editCommission", method = RequestMethod.POST)
    @ApiOperation(value = "平台编辑抽成", httpMethod = "POST")
    public ResponseData<Boolean> editCommission(@RequestBody AdminCommissionParam commissionParam) {
        SysBusinessOrganizationDto regionalAgentDto = new SysBusinessOrganizationDto();
        BeanUtils.copyProperties(commissionParam, regionalAgentDto);
        regionalAgentDto.setLevel(ORGANIZATION_TYPE_APP);
        regionalAgentDto.setOrganizationUuid(commissionParam.getAppid());
        regionalAgentDto.setUpdateBy(commissionParam.getUserUuid());
        regionalAgentDto.setStatus(CommonConstant.STATUS_OPENED);
        return sysBusinessOrganizationService.edit(regionalAgentDto);
    }

    @RequestMapping(value = "/getCommission", method = RequestMethod.POST)
    @ApiOperation(value = "平台编辑抽成", httpMethod = "POST")
    public ResponseData<SysBusinessOrganizationDto> getCommission(@RequestBody AdminParam adminParam) {
        SysBusinessOrganizationDto regionalAgentDto = new SysBusinessOrganizationDto();
        regionalAgentDto.setLevel(ORGANIZATION_TYPE_APP);
        regionalAgentDto.setOrganizationUuid(adminParam.getAppid());
        regionalAgentDto.setStatus(CommonConstant.STATUS_OPENED);
        return sysBusinessOrganizationService.get(MapUtils.convertObjToMap(regionalAgentDto));
    }


}
